//
//  FoundViewController.m
//  MMLOOK
//
//  Created by dllo on 15/9/7.
//  Copyright (c) 2015年 武俊娥. All rights reserved.
//

#import "FoundViewController.h"
#import "AsynConnect.h"
#import "SearchCollectionViewCell.h"
#import "UIImageView+WebCache.h"
#import "FoundDetailViewController.h"
#import "MBProgressHUD.h"

@interface FoundViewController ()<UICollectionViewDataSource, UICollectionViewDelegate,UICollectionViewDelegateFlowLayout, UIAlertViewDelegate>

@property (nonatomic, retain)UICollectionView *collection;
@property (nonatomic, retain)NSMutableArray *array;
@property (nonatomic, retain)NSMutableArray *collectArray;
@property (nonatomic, retain)NSMutableDictionary *seconDic;
@property (nonatomic, retain) MBProgressHUD *mbProView;

@end

@implementation FoundViewController

- (void)dealloc
{
    [_seconDic release];
    [_collectArray release];
    [_array release];
    [_collection release];
    [_mbProView release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.array = [NSMutableArray array];
        self.collectArray = [NSMutableArray array];
        self.seconDic = [NSMutableDictionary dictionary];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    self.navigationItem.title = @"全部宝贝";
    [self Connection];
    [self creatCollection];
}

- (void)Connection
{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:_searchtext forKey:@"keyword"];
    [dic setObject:@"20" forKey:@"limit"];
    [dic setObject:@"0" forKey:@"offset"];
    [dic setObject:@"" forKey:@"sort"];
    
    [self createMbProView];
    
    [AsynConnect asynConnectWithUrlstr:@"http://api.liwushuo.com/v1/search/item" parmater:dic block:^(NSData *data) {
        
        if (data) {
            
            NSMutableDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *littleDic = [bigDic objectForKey:@"data"];
            NSArray *arr = [littleDic objectForKey:@"items"];
            [self.collectArray addObjectsFromArray:arr];
            
            if (_collectArray.count == 0) {
                
                NSString *str = [NSString stringWithFormat:@"亲，未找到与'%@'相关的内容", _searchtext];
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
                [alert show];
            }
            
            [_collection reloadData];
            
            [_mbProView hide:YES];
        }
    }];
}

- (void)creatCollection
{
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    layout.itemSize = CGSizeMake((self.view.frame.size.width - 45) / 2, (self.view.frame.size.width - 45) * 1.2 / 2);
    layout.minimumInteritemSpacing = 15;
    layout.minimumLineSpacing = 15;
    layout.sectionInset = UIEdgeInsetsMake(15, 15, 15, 15);
    
    self.collection = [[[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 104) collectionViewLayout:layout] autorelease];
    _collection.dataSource = self;
    _collection.delegate = self;
    [self.view addSubview:_collection];
    _collection.backgroundColor = [UIColor whiteColor];
    [_collection registerClass:[SearchCollectionViewCell class] forCellWithReuseIdentifier:@"search"];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.collectArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    SearchCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"search" forIndexPath:indexPath];
    NSDictionary *dic = [self.collectArray objectAtIndex:indexPath.item];
    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:[dic objectForKey:@"cover_image_url"]]placeholderImage:[UIImage imageNamed:@"home.jpg"]];
    cell.priceLable.text = [NSString stringWithFormat:@"￥%@", [dic objectForKey:@"price"]];
    cell.likeLable.text = [NSString stringWithFormat:@"⭐️%@", [dic objectForKey:@"likes_count"]];
    cell.text = self.searchtext;
    cell.backgroundColor = [UIColor whiteColor];
    NSMutableAttributedString *dataStr = [cell colorData:[dic objectForKey:@"name"]];
    cell.detailLable.attributedText = dataStr;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    FoundDetailViewController *found = [[[FoundDetailViewController alloc] init] autorelease];
    found.foundDic = [self.collectArray objectAtIndex:indexPath.item];
    
    [self.navigationController pushViewController:found animated:YES];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)createMbProView
{
    self.mbProView = [[[MBProgressHUD alloc] initWithView:self.view] autorelease];
    _mbProView.mode = MBProgressHUDModeIndeterminate;
    _mbProView.labelText = @"加载中. . .";
    [_mbProView show:YES];
    [self.view addSubview:_mbProView];
    [_mbProView hide:YES afterDelay:20];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
